import 'package:aitao_flutter/const/common_const.dart';
import 'package:aitao_flutter/model/user_data.dart';
import 'package:aitao_flutter/request/api.dart';
import 'package:aitao_flutter/request/requestManager.dart';
import 'package:aitao_flutter/res/r.dart';
import 'package:aitao_flutter/routes/routes.dart';
import 'package:aitao_flutter/utils/common_utils.dart';
import 'package:aitao_flutter/utils/sp_utils.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class MinePageController extends GetxController {
  UserData? userData;
  int friendCount = 0;
  int coin = 0;
  List<Map> menuListData = [];

  var tabListData = [
    {'title': '在线客服', 'img': ImagePath.assetsProfileZaixiankefu},
    {'title': '美颜设置', 'img': ImagePath.assetsProfileMeiyanshezhi},
    {'title': '账单明细', 'img': ImagePath.assetsProfileZhangdanmingxi},
    {'title': '视频勿扰', 'img': ImagePath.assetsProfileShipinwurao},
    {'title': '消息勿扰', 'img': ImagePath.assetsProfileXiaoxiwurao},
    {'title': '系统设置', 'img': ImagePath.assetsProfileXitongshezhi},
    {'title': '隐私设置', 'img': ImagePath.assetsProfileYinsishezhi},
  ];

  @override
  void onInit() {
    super.onInit();
    debugPrint("MinePageController init ....");
    getUserData();
    getFriendList();
    getSunData();
  }

  void getUserData() async {
    var result = await RequestManager().post(API.myInfo);
    if (result != null && result['code'] == 0) {
      userData = UserData.fromJson(result['data']);
      SpUtil.saveUserData(userData!);
      setMenuListData();
      update();
    }
  }

  /// 获取钱包数据
  void getSunData() async {
    var result = await RequestManager().post(API.wallInfoData);
    if (result != null && result['code'] == 0) {
      // WJLogUtils.wjLog(result);
      Map data = result['data'];
      coin = data['account']['coin'];
      update();
    }
  }

  /// 获取好友个数
  void getFriendList() async {
    var result =
        await RequestManager().post(API.getFriendshipList, params: {'page': 1});
    if (result != null && result['code'] == 0) {
      Map data = result['data'];
      friendCount = data['total'];
      update();
    }
  }

  String getVipLevels() {
    return CommonUtils.setVipImagePath(
        userData?.userinfo?.vip ?? 2, userData?.userinfo?.vipLevel ?? 0);
  }

  void routerClick(String title) {
    if (title == MineStringConst.systemSettingText) {
      Get.toNamed(Routes.settingPage);
    } else if (title == MineStringConst.photoAlbumText) {
      // Get.toNamed(Routes.photoAlbumPage);
      Get.toNamed(Routes.testPage);
    } else if (title == MineStringConst.focusText) {
      Get.toNamed(Routes.relationPage, parameters: {'index': '0'});
    } else if (title == MineStringConst.fansText) {
      Get.toNamed(Routes.relationPage, parameters: {'index': '1'});
    } else if (title == MineStringConst.friendText) {
      Get.toNamed(Routes.relationPage, parameters: {'index': '2'});
    } else if (title == MineStringConst.seeMeText) {
      Get.toNamed(Routes.relationPage, parameters: {'index': '3'});
    } else if (title == MineStringConst.rechargeText) {
      Get.toNamed(Routes.rechargePage);
    } else if (title == MineStringConst.memberText) {
      Get.toNamed(Routes.memberPage);
    } else if (title == MineStringConst.homeText) {
      Get.toNamed(Routes.personalPage,
          parameters: {'userId': '${userData?.userinfo?.userId}'});
    } else if (title == MineStringConst.inviteText) {
      Get.toNamed(Routes.invitePage);
    } else if (title == MineStringConst.billdingDetailText) {
      // 账单明细
      Get.toNamed(Routes.billingDetailPage);
    } else if (title == MineStringConst.sayHelloText) {
      Get.toNamed(Routes.sayHelloPage);
    } else if (title == MineStringConst.callPriceText) {
      Get.toNamed(Routes.videoCostPage);
    } else if (title == MineStringConst.privacySetText) {
      Get.toNamed(Routes.privacySetPage);
    } else if (title == MineStringConst.withText) {
      Get.toNamed(Routes.withDrawPage);
    } else {
      debugPrint(title);
    }
  }

  void setMenuListData() {
    menuListData.clear();
    if (userData?.userinfo?.gender == 1) {
      menuListData = [
        {
          'title': MineStringConst.rechargeText,
          'img': ImagePath.assetsMenuChongzhi
        },
        {
          'title': MineStringConst.memberText,
          'img': ImagePath.assetsMenuChongzhi
        },
        {
          'title': MineStringConst.inviteText,
          'img': ImagePath.assetsMenuChongzhi
        },
        {
          'title': MineStringConst.homeText,
          'img': ImagePath.assetsMenuChongzhi
        },
      ];
      // 女
      if (userData?.userinfo?.canWithdraw == 1) {
        menuListData.add({
          'title': MineStringConst.withText,
          'img': ImagePath.assetsMenuTixian
        });
      }
      if (userData?.userinfo?.isAnchor == 1) {
        menuListData.add({
          'title': MineStringConst.callPriceText,
          'img': ImagePath.assetsMenuTonghuajiage
        });
        menuListData.add({
          'title': MineStringConst.sayHelloText,
          'img': ImagePath.assetsMenuDaZhaohu
        });
      } else {
        menuListData.add({
          'title': MineStringConst.authenticationText,
          'img': ImagePath.assetsMenuRenzheng
        });
      }
      menuListData.add({
        'title': MineStringConst.privacySetText,
        'img': ImagePath.assetsMenuYinsi
      });
    } else {
      menuListData = [
        {
          'title': MineStringConst.rechargeText,
          'img': ImagePath.assetsMenuChongzhi
        },
        {
          'title': MineStringConst.memberText,
          'img': ImagePath.assetsMenuChongzhi
        },
        {
          'title': MineStringConst.inviteText,
          'img': ImagePath.assetsMenuChongzhi
        },
        {
          'title': MineStringConst.homeText,
          'img': ImagePath.assetsMenuChongzhi
        },
      ];
      if (userData?.userinfo?.canWithdraw == 1) {
        // 男
        menuListData.add({
          'title': MineStringConst.withText,
          'img': ImagePath.assetsMenuTixian
        });
      }
    }
    update();
  }
}
